package com.intellagric.service.search.Impl;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.service.search.pojo.DataRecordVo;
import com.intellagric.service.search.dao.IftDataRecordSearchDao;
import com.intellagric.service.search.pojo.IftDataRecord;
import com.intellagric.service.search.IftDataRecordSearchService;
import com.intellagric.service.search.mapper.IftDataRecordSearchMapper;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/12/31
 * @DESC： 物联网数据记录结果索引库管理
 */
@Service
public class IftDataRecordSearchServiceImpl implements IftDataRecordSearchService {

    @Autowired
    private IftDataRecordSearchMapper dataRecordSearchMapper;

    @Autowired
    private IftDataRecordSearchDao dataRecordSearchDao;

    @Autowired
    private SolrServer solrServer;

    /**
     * 将数据导入索引库
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage importDataRecord() {
        try {
            //查询采集数据列表
            List<IftDataRecord> dataRecordList = dataRecordSearchMapper.selectDataRecordWithRelation();
            //遍历列表将数据逐条加入到document中
            for (IftDataRecord dataRecord : dataRecordList) {
                //创建文档对象
                SolrInputDocument document = new SolrInputDocument();
                //添加域 字段和数据
                document.setField("id",dataRecord.getId());
                document.setField("equipment_id",dataRecord.getEquipmentId());
                document.setField("scene_id",dataRecord.getSceneId());
                document.setField("equipment_name",dataRecord.getEquipmentName());
                document.setField("channel_NO",dataRecord.getChannelNo());
                document.setField("check_item_id",dataRecord.getCheckItemId());
                document.setField("check_item_name",dataRecord.getCheckItemName());
                document.setField("unit",dataRecord.getUnit());
                document.setField("processed_data",dataRecord.getProcessedData());
                document.setField("acquisition_time",dataRecord.getAcquisitionTime());
                document.setField("reporting_time",dataRecord.getReportingTime());
                document.setField("is_warning",dataRecord.getIsWarning());
                document.setField("is_read",dataRecord.getIsRead());
                document.setField("is_treatment",dataRecord.getIsTreatment());
                document.setField("warning_reason",dataRecord.getWarningReason());
                //将文档对象写入索引库
                solrServer.add(document);
            }
            //提交数据
            solrServer.commit();
            return ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMessage.fail("数据导入发生异常");
        }
    }

    /**
     * 分页查询采集数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult searchDatarecordPage(int page,int rows) {
        try {
            //创建SolrQuery对象
            SolrQuery query = new SolrQuery();
            //设置查询条件
            query.setQuery("*:*");
            //设置分页
            query.setStart(page);
            query.setRows(rows);
            //降序排序
            query.setSort("acquisition_time", SolrQuery.ORDER.desc);
            //执行查询
            return dataRecordSearchDao.search(query);
        } catch (Exception e) {
            e.printStackTrace();
            return LayuiDataGridResult.fail();
        }
    }

    /**
     * 多条件分页查询采集数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult searchDatarecordPageByVo(DataRecordVo dataRecordVo, int page, int rows) {
        try {
            //创建SolrQuery对象
            SolrQuery query = new SolrQuery();
            //设置查询条件
            StringBuilder params = new StringBuilder();
            if (dataRecordVo.getSceneId() != null && !dataRecordVo.getSceneId().equals("")) {
                params.append("scene_id:" + dataRecordVo.getSceneId());
            }
            if (dataRecordVo.getEquipmentId() != null && !dataRecordVo.getEquipmentId().equals("")) {
                params.append("equipment_id:" + dataRecordVo.getEquipmentId());
            }
            if (dataRecordVo.getCheckItemId() != null && !dataRecordVo.getCheckItemId().equals("")) {
                params.append(" AND check_item_id:" + dataRecordVo.getCheckItemId());
            }
            if (dataRecordVo.getUpAcquisitionTime() != null && dataRecordVo.getDownAcquisitionTime() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                params.append(" AND acquisition_time:[" + dateFormat.format(dataRecordVo.getDownAcquisitionTime()) + " TO " + dateFormat.format(dataRecordVo.getUpAcquisitionTime()) + "]");
            }
            query.setQuery(params.toString());
            //设置分页
            query.setStart(page);
            query.setRows(rows);
            //降序排序
            query.setSort("acquisition_time", SolrQuery.ORDER.desc);
            //执行查询
            return dataRecordSearchDao.search(query);
        } catch (Exception e) {
            e.printStackTrace();
            return LayuiDataGridResult.fail();
        }

    }

    /**
     * 多条件查询采集数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult searchDatarecordByVo(DataRecordVo dataRecordVo) {
        try {
            //创建SolrQuery对象
            SolrQuery query = new SolrQuery();
            //设置查询条件
            StringBuilder params = new StringBuilder();
            if (dataRecordVo.getEquipmentId() != null && !dataRecordVo.getEquipmentId().equals("")) {
                params.append("equipment_id:" + dataRecordVo.getEquipmentId());
            }
            if (dataRecordVo.getCheckItemId() != null && !dataRecordVo.getCheckItemId().equals("")) {
                params.append(" AND check_item_id:" + dataRecordVo.getCheckItemId());
            }
            if (dataRecordVo.getUpAcquisitionTime() != null && dataRecordVo.getDownAcquisitionTime() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
                params.append(" AND acquisition_time:[" + dateFormat.format(dataRecordVo.getDownAcquisitionTime()) + " TO " + dateFormat.format(dataRecordVo.getUpAcquisitionTime()) + "]");
            }
            query.setQuery(params.toString());
            //降序排序
            query.setSort("acquisition_time", SolrQuery.ORDER.desc);
            //执行查询
            return dataRecordSearchDao.search(query);
        } catch (Exception e) {
            e.printStackTrace();
            return LayuiDataGridResult.fail();
        }

    }

}
